package com.neu.controller;

import com.neu.constant.ResultCodeEnum;
import com.neu.pojo.Material;
import com.neu.service.FileService;
import com.neu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(value = "/file", produces = {"application/json;charset=UTF-8"})
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile file,
                         @RequestParam("pid") Integer pid,
                         @RequestParam("type") Integer type,
                         @RequestParam("name") String name,
                         @RequestParam("tag") String tag) {
        log.info("上传文件: {},{},{},{},{}",file.getSize(),pid,type,name,tag);
        Material material = Material.builder().pid(pid).type(type).name(name).tag(tag).build();
        return fileService.uploadFile(file,material);
    }

    @PostMapping("/uploadImg")
    @ResponseBody
    public Object uploadImg(@RequestParam("file") MultipartFile file) {
        log.info("上传图片: {},",file);
        if (file.isEmpty()) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return fileService.uploadImg(file);
    }
}
